package com.stylefeng.guns.modular.vpn_server_info.service.impl;

import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.model.VpnServer;
import com.stylefeng.guns.modular.system.dao.VpnServerMapper;
import com.stylefeng.guns.modular.vpn.config.properties.SftpProperties;
import com.stylefeng.guns.modular.vpn.utils.FileUtil;
import com.stylefeng.guns.modular.vpn.utils.LogUtil;
import com.stylefeng.guns.modular.vpn.utils.ServerIPUtil;
import com.stylefeng.guns.modular.vpn_server_info.entity.VpnServerDTO;
import com.stylefeng.guns.modular.vpn_server_info.service.IVpnAllowIpService;
import com.stylefeng.guns.modular.vpn_server_info.service.IVpnServerService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-03-02
 */
@Service
@Slf4j
public class VpnServerServiceImpl extends ServiceImpl<VpnServerMapper, VpnServer> implements IVpnServerService {

    @Autowired
    private IVpnAllowIpService vpnAllowIpService;

    @Autowired
    private VpnServerMapper vpnServerMapper;

    @Resource(name = "imageSftp")
    private SftpProperties imageSftp;

    /**
     * 添加VPN服务节点
     * @param vpnServerDTO
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void addServerPeer(VpnServerDTO vpnServerDTO) {
        log.info("添加VPN 服务器节点 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        VpnServer vpnServer = new VpnServer();
        BeanUtils.copyProperties(vpnServerDTO,vpnServer);
        vpnServer.setCreateTime(new Date());
        vpnServer.setUpdateTime(new Date());
        vpnServer.setAreaLevel(vpnServerDTO.getAreaLevel());

        log.info("检察VPN服务器网段是否正确 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        boolean bool =  ServerIPUtil.isTrueOfSegment(vpnServerDTO.getBeginSegment(),vpnServerDTO.getEndSegment());
        if(!bool){
            log.error("添加VPN服务器错误: {}",BizExceptionEnum.VPN_SERVER_IP_SEGMENT_ERROR.getMessage());
            throw new GunsException(BizExceptionEnum.VPN_SERVER_IP_SEGMENT_ERROR);
        }
        log.info("VPN服务器网段检测正常 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        if(vpnServerDTO.getFile() == null){
            throw new GunsException(BizExceptionEnum.VPN_SERVER_ICON_NOT_EXIST);
        }
        //上传图片
        String remoteFile = FileUtil.uploadFile(vpnServerDTO.getFile(),imageSftp);
        if(StringUtils.isEmpty(remoteFile)){
            log.error(BizExceptionEnum.VPN_UPLOAD_FILE_FAIL.getMessage());
            throw new GunsException(BizExceptionEnum.VPN_UPLOAD_FILE_FAIL);
        }

        log.info("完成VPN 服务器图标的上传 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        vpnServer.setSourcePath(remoteFile);

        boolean bol = this.insert(vpnServer);
        if(bol){
            vpnServerDTO.setServerId(vpnServer.getId());
            vpnAllowIpService.insertAllowIPs(vpnServerDTO);
        }

        LogUtil.saveOperationLog("添加VPN服务器节点","添加IP："+vpnServer.getServerIp());
        log.info("添加VPN 服务器节点完成 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    /**
     * 服务器禁用、启用
     * @param serverId
     * @param status
     */
    @Override
    @Transactional
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void updateServerPeer(Integer serverId,Integer status) {
        VpnServer vpnServer = this.selectById(serverId);
        vpnServer.setStatus(status);
        boolean flag = updateById(vpnServer);
        if(status == 0 && flag){
            vpnServerMapper.deleteConnectInfo(serverId);
        }
    }

    /**
     * 修改VPN服务器信息
     * @param vpnServerDTO
     */
    @Override
    @Transactional
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void updateServer(VpnServerDTO vpnServerDTO) {
        log.info("更新VPN 服务器信息 开始 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        VpnServer vpnServer = baseMapper.selectById(vpnServerDTO.getServerId());
        if(vpnServer == null){
            throw new GunsException(BizExceptionEnum.VPN_SERVER_IS_EXIST);
        }
        //上传图片
        if(vpnServerDTO.getFile() != null){
            String remoteFile = FileUtil.uploadFile(vpnServerDTO.getFile(),imageSftp);
            if(StringUtils.isEmpty(remoteFile)){
                throw new GunsException(BizExceptionEnum.VPN_UPLOAD_FILE_ERROR);
            }
            log.info("完成VPN服务器图标更新 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            vpnServer.setSourcePath(remoteFile);
        }
        vpnServer.setUpdateTime(new Date());
        vpnServer.setServerType(vpnServerDTO.getServerType());
        vpnServer.setServerIp(vpnServerDTO.getServerIp());
        vpnServer.setServerName(vpnServerDTO.getServerName());
        vpnServer.setServerName2(vpnServerDTO.getServerName2());
        vpnServer.setServerName3(vpnServerDTO.getServerName3());
        vpnServer.setServerPort(vpnServerDTO.getServerPort());
        vpnServer.setPubKey(vpnServerDTO.getPubKey());
        vpnServer.setAreaLevel(vpnServerDTO.getAreaLevel());
        baseMapper.updateById(vpnServer);
        log.info("更新VPN 服务器信息 结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object serverList() {
        return baseMapper.serverList();
    }
}
